package com.xiaotu.chessAI;

import cn.hutool.core.util.RandomUtil;

/**
 * @author zhangxiao
 * @create 2022-08-03 16:39
 */
public class RedCampFirstStep {

    private static int[] init_board = {
            1, 3, 5, 7, 16, 8, 6, 4, 2,
            0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 9, 0, 0, 0, 0, 0, 10, 0,
            11, 0, 12, 0, 13, 0, 14, 0, 15,
            0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0,
            27, 0, 28, 0, 29, 0, 30, 0, 31,
            0, 25, 0, 0, 0, 0, 0, 26, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0,
            17, 19, 21, 23, 32, 24, 22, 20, 18
    };

    void m_print(int[] result) {
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 9; j++) {
                if (result[i * 9 + j] > 9)
                    System.out.print(result[i * 9 + j]);
                else
                    System.out.print(" " + result[i * 9 + j]);
                System.out.print(" ");
            }
            System.out.println("");
        }
    }

    private static int[] redChe(int type) {
        Integer[] che1 = new Integer[]{9, 18};
        Integer[] che2 = new Integer[]{24, 26};
        int[] clone = init_board.clone();
        switch (type) {
            case 1:
                clone[0] = 0;
                clone[RandomUtil.randomEle(che1)] = type;
                break;
            case 2:
                clone[8] = 0;
                clone[RandomUtil.randomEle(che2)] = type;
                break;
        }
        return clone;
    }

    private static int[] redMa(int type) {
        Integer[] ma1 = new Integer[]{18, 20};
        Integer[] ma2 = new Integer[]{17, 26};
        int[] clone = init_board.clone();
        switch (type) {
            case 3:
                clone[1] = 0;
                clone[RandomUtil.randomEle(ma1)] = type;
                break;
            case 4:
                clone[7] = 0;
                clone[RandomUtil.randomEle(ma2)] = type;
                break;
        }
        return clone;
    }

    private static int[] redXiang(int type) {
        Integer[] xiang1 = new Integer[]{18, 22};
        Integer[] xiang2 = new Integer[]{22, 26};
        int[] clone = init_board.clone();
        switch (type) {
            case 5:
                clone[2] = 0;
                clone[RandomUtil.randomEle(xiang1)] = type;
                break;
            case 6:
                clone[6] = 0;
                clone[RandomUtil.randomEle(xiang2)] = type;
                break;
        }
        return clone;
    }

    private static int[] redShi(int type) {
        int[] clone = init_board.clone();
        switch (type) {
            case 7:
                clone[3] = 0;
                break;
            case 8:
                clone[5] = 0;
                break;
        }
        clone[13] = type;
        return clone;
    }

    private static int[] redShuai() {
        int[] clone = init_board.clone();
        clone[4] = 0;
        clone[13] = 4;
        return clone;
    }

    private static int[] redPao(int type) {
        Integer[] pao1 = new Integer[]{18, 20, 21, 22, 23, 24, 28, 37, 46, 55};
        Integer[] pao2 = new Integer[]{20, 21, 22, 23, 24, 26, 34, 43, 52, 61};
        int[] clone = init_board.clone();
        switch (type) {
            case 9:
                clone[19] = 0;
                clone[RandomUtil.randomEle(pao1)] = type;
                break;
            case 10:
                clone[25] = 0;
                clone[RandomUtil.randomEle(pao2)] = type;
                break;
        }
        return clone;
    }

    private static int[] redBing(int type) {
        Integer[] bing1 = new Integer[]{36};
        Integer[] bing2 = new Integer[]{38};
        Integer[] bing3 = new Integer[]{40};
        Integer[] bing4 = new Integer[]{42};
        Integer[] bing5 = new Integer[]{44};
        int[] clone = init_board.clone();
        switch (type) {
            case 11:
                clone[27] = 0;
                clone[RandomUtil.randomEle(bing1)] = type;
                break;
            case 12:
                clone[29] = 0;
                clone[RandomUtil.randomEle(bing2)] = type;
                break;
            case 13:
                clone[31] = 0;
                clone[RandomUtil.randomEle(bing3)] = type;
                break;
            case 14:
                clone[33] = 0;
                clone[RandomUtil.randomEle(bing4)] = type;
                break;
            case 15:
                clone[35] = 0;
                clone[RandomUtil.randomEle(bing5)] = type;
                break;
        }
        return clone;
    }

    public static String redFirstStep() {
        int piece = RandomUtil.randomInt(1, 17);
        if (piece == 1 || piece == 2)
            return vec_to_str(redChe(piece));
        if (piece == 3 || piece == 4)
            return vec_to_str(redMa(piece));
        if (piece == 5 || piece == 6)
            return vec_to_str(redXiang(piece));
        if (piece == 7 || piece == 8)
            return vec_to_str(redShi(piece));
        if (piece == 9 || piece == 10)
            return vec_to_str(redPao(piece));
        if (piece == 11 || piece == 12 || piece == 13 || piece == 14 || piece == 15)
            return vec_to_str(redBing(piece));
        if (piece == 16)
            return vec_to_str(redShuai());
        return null;
    }

    private static String vec_to_str(int[] v) {
        String result = "";
        for (int i = 0; i < v.length; i++)
            result = result + v[i] + ".";
        return result;
    }

}
